using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;


namespace EEst.Tools
{
    class XCUtil
    {
        private string nmakePath = null;

        private string makefilePath = "";
        public string MakefilePath
        {
            get { return makefilePath; }
            set { makefilePath = value; }
        }

        private string makefileName = "Makefile.xc";
        public string MakefileName
        {
            get { return makefileName; }
            set { makefileName = value; }
        }

        private StringWriter stdout = null;
        public StringWriter Stdout
        {
            get { return stdout; }
            set { stdout = value; }
        }

        private StringWriter stderr = null;
        public StringWriter Stderr
        {
            get { return stderr; }
            set { stderr = value; }
        }

        public XCUtil() 
        {
            searchVCPath();
        }
        
        private void searchVCPath()
        {
            IDictionary dic = System.Environment.GetEnvironmentVariables();
            foreach (System.Collections.DictionaryEntry de in dic)
            {
                string key = de.Key as string;
                if (key.CompareTo("_ACP_PATH") == 0)
                {
                    string value = de.Value as string;
                    string[] paths = value.Split(new char[] { ';' });
                    for (int i = 0; i < paths.Length; i++)
                    {
                        if (paths[i].EndsWith("\\VC\\bin"))
                        {
                            nmakePath = paths[i];
                            break;
                        }
                    }
                }
                if (nmakePath != null)
                {
                    break;
                }
            }
        }

        public int Make(string target)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = nmakePath + "\\nmake";
            process.StartInfo.Arguments = "/nologo /f " + MakefileName + " " + target;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.WorkingDirectory = MakefilePath;
            process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(
                delegate(object sender, System.Diagnostics.DataReceivedEventArgs e) { 
                    if (e.Data != null && Stdout != null)
                        Stdout.WriteLine(e.Data);
                }
            );
            process.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(
                delegate(object sender, System.Diagnostics.DataReceivedEventArgs e) {
                    if (e.Data != null && Stderr != null)
                        Stderr.WriteLine(e.Data);
                }
            );

            try {
                process.Start();
                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
                process.WaitForExit();
            }
            catch (Exception ex) {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return -1;
            } 
            return process.ExitCode;
        }
    }
}
